<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\Cnarea\controller;

use plugin\saiadmin\basic\BaseController;
use app\Cnarea\logic\CnareaLogic;
use app\Cnarea\validate\CnareaValidate;
use support\Request;
use support\Response;

/**
 * 地区管理控制器
 */
class CnareaController extends BaseController
{
    
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->logic = new CnareaLogic();
        $this->validate = new CnareaValidate;
        parent::__construct();
    }

    /**
     * 数据列表
     * @param Request $request
     * @return Response
     */
    public function index(Request $request): Response
    {
        $where = $request->more([
            ['level', ''],
            ['name', ''],
        ]);
        $data = $this->logic->tree($where);
        return $this->success($data);
    }
    public function update(Request $request, $id) : Response
    {
        $id = $request->input('id', $id);
        $data = $request->post();
        if ($this->validate) {
            if (!$this->validate->scene('update')->check($data)) {
                return $this->fail($this->validate->getError());
            }
        }
        unset($data['id']);
        $info = $this->logic->where(['area_code' => $id])->find();

        if (!$info) {
            return $this->fail('没有找到该数据');
        }
        $result = $this->logic->update($data, ['area_code' => $id]);
        if ($result) {
            $this->afterChange('update');
            return $this->success('操作成功');
        } else {
            return $this->fail('操作失败');
        }
    }
    public function destroy(Request $request) : Response
    {
        $ids = $request->input('ids', '');
        
        //存在子级不能被删除
        // $ids = explode(',', $ids);
        
        $num = $this->logic->where('parent_code', 'in', $ids)->count();
        if ($num > 0) {
            return $this->fail('存在子级不能被删除');
        }
        $ids=$this->logic->where('area_code', 'in', $ids)->column('id');
       
        if (!empty($ids)) {
           
            // $ids=explode(',', $ids);
            $this->logic->destroy($ids);
            $this->afterChange('destroy');
            return $this->success('操作成功');
        } else {
            return $this->fail('参数错误，请检查');
        }
    }
    public function getSelect(Request $request) : Response
    {
        $where = $request->more([
            ['level', ''],
            ['parent_code','']
           
        ]);
        $query = $this->logic->search($where)->field('name as label,area_code as value');
        $data = $this->logic->getAll($query);
        switch ($where['level']) {
            case 1:
                 $name='全国';            
                break;
           case 2:
                $name='全省';
                break;
            case 3:
                $name='全市';
                break;     
        }
        if($data){
            array_unshift($data, ['label' => $name, 'value' => 0]);
        }else{
            $data = [['label' => $name, 'value' => 0]];
        }
       
        return $this->success($data);
    }


}
